<?php

namespace app\admin\model;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

class OrderChange extends BaseModel
{

    use SoftDelete;

    

    // 表名
    protected $name = 'order_change';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
    ];

    //订单状态处理
    public static function fetchStatus($status = null){
        $data =  [
            1 => '待支付',
            2 => '已支付',
            3 => '已取消',
            4 => '已退款',
        ];
        if (isset($data[$status])) {
            return $data[$status];
        }else{
            return $data;
        }
    }

    //支付方式
    public static function fetchPayType($pay_type = null){
        $data =  [
            1 => '微信支付',
            2 => '支付宝支付',
        ];
        if (isset($data[$pay_type])) {
            return $data[$pay_type];
        }else{
            return $data;
        }
    }

    //换车方式
    public static function fetchType($pay_type = null){
        $data =  [
            1 => '无偿换车',
            2 => '有偿换车',
        ];
        if (isset($data[$pay_type])) {
            return $data[$pay_type];
        }else{
            return $data;
        }
    }

    public function order(){
        return $this->hasOne('order', 'id', 'order_id')->joinType('left');
    }
    public function user(){
        return $this->hasOne('user', 'id', 'user_id')->joinType('left');
    }
    public function merchantCar(){
        return $this->hasOne('merchant_car', 'id', 'merchant_car_id')->joinType('left');
    }
    public function users(){
        return $this->hasOne('user', 'id', 'user_id')->bind(['username','avatar','truename','id_card'])->joinType('left');
    }
    public function merchantCars(){
        return $this->hasOne('merchant_car', 'id', 'merchant_car_id')->bind(['car_id','merchant_car_name','car_number','car_label', 'handle_fee', 'night_pick_fee', 'night_return_fee'])->joinType('left');
    }

    public function merchants(){
        return $this->hasOne('merchant', 'id', 'merchant_id')->bind(['merchant_phone','shop_logo','shop_name','car_label'])->joinType('left');
    }

    //生成唯一退款号
    public static function getOutRefundNo(){
        $out_refund_no = sys_create_orderno(10);
        $order = self::where('out_refund_no', $out_refund_no)->count();
        if($order){
            return self::getOutRefundNo();
        }
        return $out_refund_no;
    }

    //生成唯一订单号
    public static function getOutTradeNo(){
        $out_refund_no = sys_create_orderno(6);
        $order = self::where('out_trade_no', $out_refund_no)->count();
        if($order){
            return self::getOutRefundNo();
        }
        return $out_refund_no;
    }
}
